package com.apes.fn.scm.saleReport.service;

import com.apes.fn.scm.internal.help.model.InternalHelpReturnApproval;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.saleReport.repository.CostDetailsRepository;
import com.apes.fn.scm.saleReport.repository.TurnoverDetailsRepository;
import com.apes.fn.scm.vipSettle.model.VipUnionSettle;
import com.apes.fn.scm.vipSettle.repository.VipUnionSettleRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.framework.util.LogicalUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.sal.saleOrder.SaleOrder;
import com.apes.scm.sal.saleReturn.SaleReturn;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherExtension;
import com.apes.scm.voucher.model.VoucherFinExtension;
import com.apes.scm.voucher.model.VoucherInventory;
import com.apes.scm.voucher.repository.VoucherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SaleReportSubscriberService {
    @Autowired
    private SaleReportService saleReportService;
    @Autowired
    private TurnoverDetailsRepository turnoverDetailsRepository;
    @Autowired
    private CostDetailsRepository costDetailsRepository;
    @Autowired
    private VoucherRepository voucherRepository;

//    @OutboxSubscriber(id = "sale_report_retail_order", name = "零售单写入营业额明细", condition = "orderType != 'DS0' && orderType != 'lossQuotation' && orderType != 'directPay' && orderType != 'noDirectPay'")
//    public void retailOrder(RetailOrder retailOrder) {
//        //重复判断
//        if (check(retailOrder.getId())) return;
//        Map<String, Object> map = MapUtil.mapped(retailOrder);
//        map.put("ORDER_TYPE", "retailOrder");
//        SimpleRequest simpleRequest = new SimpleRequest();
//        simpleRequest.setData(map);
//        saleReportService.createSaleReport(simpleRequest);
//    }

//    @OutboxSubscriber(id = "sale_report_sale_order", name = "分销单写入营业额明细")
//    public void saleOrder(SaleOrder saleOrder) {
//        //重复判断
//        if (check(saleOrder.getId())) return;
//        Map<String, Object> map = MapUtil.mapped(saleOrder);
//        map.put("ORDER_TYPE", "saleOrder");
//        SimpleRequest simpleRequest = new SimpleRequest();
//        simpleRequest.setData(map);
//        saleReportService.createSaleReport(simpleRequest);
//    }

    @OutboxSubscriber(id = "sale_report_sale_return", name = "分销退货单写入营业额明细")
    public void saleReturn(SaleReturn saleReturn) {
        //重复判断
        if (check(saleReturn.getId())) return;
        Map<String, Object> map = MapUtil.mapped(saleReturn);
        map.put("ORDER_TYPE", "saleReturn");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(map);
        saleReportService.createSaleReport(simpleRequest);
    }

    @OutboxSubscriber(id = "sale_report_internal_help_return", name = "内协退货写入营业额明细", condition = "state == 'done'")
    public void internalHelpReturnApproval(InternalHelpReturnApproval internalHelpReturnApproval) {
        //重复判断
        if (check(internalHelpReturnApproval.getId())) return;
        Map<String, Object> map = MapUtil.mapped(internalHelpReturnApproval);
        map.put("ORDER_TYPE", "internalHelpReturnApproval");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(map);
        saleReportService.createSaleReport(simpleRequest);
    }

    @OutboxSubscriber(id = "sale_report_acc_voucher", name = "业务凭证写入营业额与成本明细")
    public void voucher(VoucherFinExtension voucherFinExtension) {
        Voucher voucher = voucherRepository.findOne(voucherFinExtension.getVoucher().getId());
        if("CUSTOMER_SALE".equals(voucher.getVoucherType()) && "IM_OWN".equals(voucher.getInventoryMode())) {
            //重复判断
            if (check(voucher.getId())) return;
            Map<String, Object> map = MapUtil.mapped(voucher);
            map.put("ORDER_TYPE", "voucher");
            SimpleRequest simpleRequest = new SimpleRequest();
            simpleRequest.setData(map);
            saleReportService.createSaleReport(simpleRequest);
        }
    }

    @OutboxSubscriber(id = "sale_report_vip_union", name = "会员联盟写入营业额明细", condition = "paymentMethodType == 'discount' && type == 'out'")
    public void vipUnionSettle(VipUnionSettle vipUnionSettle) {
        //重复判断
        if (check(vipUnionSettle.getId())) return;
        Map<String, Object> map = MapUtil.mapped(vipUnionSettle);
        map.put("ORDER_TYPE", "vipUnionSettle");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(map);
        saleReportService.createSaleReport(simpleRequest);
    }

    private boolean check(String origin){
        if (turnoverDetailsRepository.findByOrigin(origin).size() != 0) return true;
        if (costDetailsRepository.findByOrigin(origin).size() != 0) return true;
        return false;
    }
}
